<?php
declare (strict_types = 1);

namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
ini_set('default_socket_timeout', '-1');  //不超时
class Gopush extends Command{


    protected function configure()
    {
        // 指令配置
        $this->setName('orderqueue')->setDescription('延迟队列任务进程');
//        $this->addArgument(self::COMMAND_ARGV_1);
    }

    protected function execute(Input $input, Output $output)
    {


        $redis = new \fast\Redis();
        $redis->setOption();
        // $redis->psubscribe(array('__keyevent@0__:expired'), 'keyCallback');
        $redis->psubscribe(array('__keyevent@14__:expired'), function ($redis, $pattern, $channel, $msg){
            $msg = explode(':', $msg);
            //预约
            if($msg[0] == 'yu_wzf_exp'){
                $yyorderid = Db::name('reservation_item') -> where('r_ordersn',$msg[1]) -> find();
                if(empty($yyorderid)){
                    return false;
                }
                if($yyorderid['status'] == 1){
                    Db::name('reservation_item') -> where('r_ordersn',$msg[1]) -> update(['status'=>4,'updatetime'=>time(),'deletetime'=>time()]);
                    Db::name('reservation') -> where('id',$yyorderid['reservationid']) -> dec('reservationsnum',$yyorderid['re_num']) -> update(['updatetime'=>time()]);
                    $redis -> del($msg);
                }
            }
            if($msg[0] == 'yu_zf_exp'){
                $yyorderid = Db::name('reservation_item') -> where('r_ordersn',$msg[1]) -> find();
                if(empty($yyorderid)){
                    return false;
                }
                if($yyorderid['status'] == 2){
                    Db::name('reservation_item') -> where('r_ordersn',$msg[1]) -> update(['status'=>3,'updatetime'=>time()]);
                    $redis -> del($msg);
                }
            }

            if($msg[0] == 'order_wzf_exp'){
                $yyorderid = Db::name('orders') -> where('order_sn',$msg[1]) -> find();
                if(empty($yyorderid)){
                    return false;
                }
                if($yyorderid['status'] == 1){
                    $orderid = Db::name('orderitem') -> where('orderid',$yyorderid['id']) -> find();
                    Db::name('orders') -> where('order_sn',$msg[1]) -> update(['status'=>9,'updatetime'=>time(),'deletetime'=>time()]);
                    Db::name('product')-> where('id', $orderid['proid']) -> inc('stock',$orderid['quantity'])-> dec('sales',$yyorderid['quantity']) -> update();
                    $redis -> del($msg);
                }
            }



            //................

            /*TODO处理业务逻辑*/
//            try {
//                //处理业务逻辑
//
//            } catch (\Exception $e) {
//
//            }
        });
        $output->writeln("TestCommand:998998998");
    }

}